import request from '@/utils/request'
import type {
  CourseReview,
  CourseReviewListResponse,
  CreateCourseReviewRequest,
  CoachRating,
  CoachRatingListResponse,
  CreateCoachRatingRequest,
  CreateReviewReplyRequest,
  Feedback,
  FeedbackListResponse,
  CreateFeedbackRequest,
  HandleFeedbackRequest,
  AuditReviewRequest,
  SetFeaturedReviewRequest,
  ReviewStatisticsResponse,
  ReportReviewRequest,
  ReviewReport
} from '@/types/api/review'
import type { BaseResponse, PaginationResponse } from '@/types/common/response'

/**
 * 课程评价相关API
 */
export const courseReviewApi = {
  /**
   * 创建课程评价
   * @param data 评价信息
   */
  createCourseReview(data: CreateCourseReviewRequest) {
    return request.post<BaseResponse<{ id: number; rating: number; content: string; status: string }>>('/reviews/courses', data)
  },

  /**
   * 获取课程评价列表
   * @param courseId 课程ID
   * @param params 查询参数
   */
  getCourseReviewList(
    courseId: number,
    params: {
      rating?: number
      hasImages?: boolean
      sort?: 'latest' | 'rating' | 'helpful'
      page?: number
      pageSize?: number
    }
  ) {
    return request.get<BaseResponse<CourseReviewListResponse>>(`/reviews/courses/${courseId}`, { params })
  },

  /**
   * 标记评价有用
   * @param id 评价ID
   */
  markHelpful(id: number) {
    return request.post<BaseResponse<null>>(`/reviews/${id}/helpful`)
  },

  /**
   * 回复评价
   * @param id 评价ID
   * @param data 回复信息
   */
  replyToReview(id: number, data: CreateReviewReplyRequest) {
    return request.post<BaseResponse<null>>(`/reviews/${id}/reply`, data)
  }
}

/**
 * 教练评分相关API
 */
export const coachRatingApi = {
  /**
   * 评价教练
   * @param data 评分信息
   */
  createCoachRating(data: CreateCoachRatingRequest) {
    return request.post<BaseResponse<{ id: number; overallRating: number }>>('/reviews/coaches', data)
  },

  /**
   * 获取教练评分
   * @param coachId 教练ID
   */
  getCoachRating(coachId: number) {
    return request.get<BaseResponse<CoachRatingListResponse>>(`/reviews/coaches/${coachId}`)
  }
}

/**
 * 意见反馈相关API
 */
export const feedbackApi = {
  /**
   * 提交反馈
   * @param data 反馈信息
   */
  createFeedback(data: CreateFeedbackRequest) {
    return request.post<BaseResponse<{ id: number; type: string; status: string }>>('/feedbacks', data)
  },

  /**
   * 获取我的反馈列表
   * @param params 查询参数
   */
  getMyFeedbackList(params: { page?: number; pageSize?: number }) {
    return request.get<BaseResponse<PaginationResponse<Feedback>>>('/feedbacks/my', { params })
  },

  /**
   * 获取反馈详情
   * @param id 反馈ID
   */
  getFeedbackDetail(id: number) {
    return request.get<BaseResponse<Feedback>>(`/feedbacks/${id}`)
  },

  /**
   * 获取所有反馈（管理员）
   * @param params 查询参数
   */
  getAllFeedbackList(params: {
    type?: 'BUG' | 'FEATURE' | 'COMPLAINT' | 'SUGGESTION' | 'OTHER'
    status?: 'PENDING' | 'PROCESSING' | 'RESOLVED' | 'CLOSED' | 'REJECTED'
    priority?: 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT'
    page?: number
    pageSize?: number
  }) {
    return request.get<BaseResponse<PaginationResponse<Feedback>>>('/feedbacks', { params })
  },

  /**
   * 处理反馈（管理员）
   * @param id 反馈ID
   * @param data 处理信息
   */
  handleFeedback(id: number, data: HandleFeedbackRequest) {
    return request.put<BaseResponse<null>>(`/feedbacks/${id}/handle`, data)
  }
}

/**
 * 评价管理相关API（管理员）
 */
export const reviewManagementApi = {
  /**
   * 审核评价
   * @param id 评价ID
   * @param data 审核信息
   */
  auditReview(id: number, data: AuditReviewRequest) {
    return request.put<null>(`/admin/reviews/${id}/audit`, data)
  },

  /**
   * 设置精选评价
   * @param id 评价ID
   * @param data 设置信息
   */
  setFeaturedReview(id: number, data: SetFeaturedReviewRequest) {
    return request.put<null>(`/admin/reviews/${id}/featured`, data)
  },

  /**
   * 隐藏评价
   * @param id 评价ID
   * @param isHidden 是否隐藏
   */
  hideReview(id: number, isHidden: boolean) {
    return request.put<null>(`/admin/reviews/${id}/hide`, { is_hidden: isHidden })
  },

  /**
   * 获取评价统计信息
   * @param params 查询参数
   */
  getReviewStatistics(params: {
    startDate?: string
    endDate?: string
    courseId?: number
    coachId?: number
  }) {
    return request.get<BaseResponse<ReviewStatisticsResponse>>('/reviews/statistics', { params })
  },

  /**
   * 举报评价
   * @param data 举报信息
   */
  reportReview(data: ReportReviewRequest) {
    return request.post<BaseResponse<null>>('/reviews/report', data)
  },

  /**
   * 获取举报列表（管理员）
   * @param params 查询参数
   */
  getReviewReports(params: {
    status?: 'PENDING' | 'RESOLVED' | 'REJECTED'
    page?: number
    pageSize?: number
  }) {
    return request.get<PaginationResponse<ReviewReport>>('/admin/reviews/reports', { params })
  },

  /**
   * 处理举报（管理员）
   * @param id 举报ID
   * @param data 处理信息
   */
  handleReport(id: number, data: { status: 'RESOLVED' | 'REJECTED'; action?: 'HIDE' | 'DELETE' }) {
    return request.put<null>(`/admin/reviews/reports/${id}/handle`, data)
  }
}
